﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Truextend.Training.Business;
using Truextend.Common.Data;
using Truextend.Training.Model.Entities;

namespace Truextend.Training.Business.Tests
{
    [TestClass]
    public class CourseInstanceManagerTest : BaseTestClass
    {
        [Priority(3), TestMethod]
        public void AddCourseInstanceTest()
        {
            int beforeInset = 0;
            int afterInsert = 0;
            CourseInstanceManager courseInstanceManager = null;
            CourseManager courseManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                courseManager = new CourseManager(uow);
                var courseInstance = new CourseInstance
                {
                    Location = "location",
                    InstructorId = 1,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddHours(1),
                    State = "AP", //approve
                    Course = courseManager.GetAllCourses().ToList().First()
                };
                
                beforeInset = courseInstanceManager.GetCount();
                courseInstanceManager.AddCourseInstance(courseInstance);
            }, () =>
            {
                afterInsert = courseInstanceManager.GetCount();
            }
                );
            Assert.AreEqual(beforeInset + 1, afterInsert);
        }


        [TestMethod]
        public void GetAllCourseInstancesAvailableTest()
        {
            IList<CourseInstance> list = null;
            CourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);

                list = courseInstanceManager.GetAllCourseInstancesAvailable();
            } );

            Console.WriteLine(list.Count);
            Assert.IsNotNull(list);
            //Assert.AreEqual(2, list.Count);
        }

        [TestMethod]
        public void GetCourseInstanceByIdTest()
        {
            CourseInstance courseInstance = null;
            CourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);

                courseInstance = courseInstanceManager.GetCourseInstanceById( courseInstanceManager.GetAllCourseInstancesAvailable().ToList().First().CourseInstanceId );
                //courseInstance = courseInstanceManager.GetCourseInstanceById(1);
            });

            Assert.IsNotNull(courseInstance);
            //Assert.AreEqual(2, courseInstance.ScheduleCourses.Count);
        }

        [TestMethod]
        public void EditCourseInstanceTest()
        {
            CourseInstanceManager courseInstanceManager = null;
            CourseInstance courseInstanceEdit = null;
            CourseInstance courseInstanceRecovery = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);
                courseInstanceEdit = courseInstanceManager.GetCourseInstanceById(courseInstanceManager.GetAllCourseInstancesAvailable().First().CourseInstanceId );
                
                courseInstanceRecovery = new CourseInstance();
                courseInstanceRecovery.CourseInstanceId = courseInstanceEdit.CourseInstanceId;
                courseInstanceRecovery.Location = courseInstanceEdit.Location;
                courseInstanceRecovery.StartDate = courseInstanceEdit.StartDate;
                courseInstanceRecovery.EndDate = courseInstanceEdit.EndDate;
                courseInstanceRecovery.State = courseInstanceEdit.State;
                courseInstanceRecovery.InstructorId = courseInstanceEdit.InstructorId;

                if (courseInstanceRecovery.Location.Equals("UPD LOCATION AGAIN"))
                {
                    courseInstanceEdit.Location = "UPD LOCATION AGAIN1";
                }
                else
                {
                    courseInstanceEdit.Location = "UPD LOCATION AGAIN";
                }

                courseInstanceManager.EditCourseInstance(courseInstanceEdit);
            });
            Assert.AreEqual(courseInstanceEdit.CourseInstanceId, courseInstanceRecovery.CourseInstanceId);
            Assert.AreNotEqual(courseInstanceEdit.Location, courseInstanceRecovery.Location);
        }

        [TestMethod]
        public void GetInstancesOneDayBeforeStartTest()
        {
            IList<CourseInstance> list = null;
            CourseInstanceManager courseInstanceManager = null;
            ExecuteManager(uow =>
            {
                courseInstanceManager = new CourseInstanceManager(uow);

                list = courseInstanceManager.GetInstancesOneDayBeforeStart();
            });

            Console.Write(list.Count);
            Assert.IsNotNull(list);
        }

    }
}
